
package com.example.administrator.mymusic;

import android.Manifest;
import android.media.MediaPlayer;
import android.os.Environment;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.os.Handler;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;


public class MainActivity extends AppCompatActivity {

    private TextView text, startText, totalText;
    private static int id = 0;
    private int currentT;
    private MediaPlayer player, play;
    private Button previous, boFang, next, stop, back;
    private ImageButton suiJi;
    private ArrayList<String> list = new ArrayList<>();
    private String sdcardPath;
    Handler handler;
    Random ra;
    private int currentTime;
    private SeekBar bar;
    private int totalTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ra = new Random();

        play = MediaPlayer.create(this, R.raw.xiao);
        play.start();


        //获取获取本地音乐
        String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/kgmusic/download/";
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE},
                124);
        //获取权限
        final File[] file = new File(sdcardPath).listFiles();

        for (int i = 0; i < file.length; i++) {
            list.add(file[i].getAbsolutePath());
        }

        player = new MediaPlayer();
        try {
            this.player.setDataSource(list.get(id));
            this.player.prepare();
        } catch (IOException ioe) {
        }

        text = (TextView) findViewById(R.id.text);
        previous = (Button) findViewById(R.id.previous);
        boFang = (Button) findViewById(R.id.boFang);
        next = (Button) findViewById(R.id.next);
        stop = (Button) findViewById(R.id.stop);
        back = (Button) findViewById(R.id.back);
        bar = (SeekBar) findViewById(R.id.bar);
        startText = (TextView) findViewById(R.id.start);
        totalText = (TextView) findViewById(R.id.total);
        suiJi = (ImageButton) findViewById(R.id.suiJi);

        bar.setProgress(0);
        totalTime = player.getDuration();
        bar.setMax(totalTime);

        if(fen(miao(totalTime)) < 10 && fenMiao(miao(totalTime)) < 10) {
            totalText.setText("0" + fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
        } else if(fen(miao(totalTime)) < 10) {
            totalText.setText("0" + fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
        } else if(fenMiao(miao(totalTime)) < 10) {
            totalText.setText(fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
        } else {
            totalText.setText(fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
        }

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //抓取消息
                currentT = msg.what;
                bar.setProgress(currentT);

                if(fen(miao(currentT)) < 10 && fenMiao(miao(currentT)) < 10) {
                    startText.setText("0" + fen(miao(currentT)) + ":0" + fenMiao(miao(currentT)));
                } else if(fen(miao(currentT)) < 10) {
                    startText.setText("0" + fen(miao(currentT)) + ":" + fenMiao(miao(currentT)));
                } else if(fenMiao(miao(currentT)) < 10) {
                    startText.setText(fen(miao(currentT)) + ":0" + fenMiao(miao(currentT)));
                } else {
                    startText.setText(fen(miao(currentT)) + ":" + fenMiao(miao(currentT)));
                }

            }

        };

        Thread thread = new Thread() {
            @Override
            public void run() {
                super.run();

                currentTime = 0;
                for (; currentTime < totalTime;) {
                    currentTime = player.getCurrentPosition();
                    Message msg = new Message();
                    msg.what = currentTime;
                    handler.sendMessage(msg);
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {

                    }
                }
            }
        };
        thread.start();

        text.setText(list.get(id).substring(34, list.get(id).length() - 4));
        boFang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!player.isPlaying()) {
                    player.start();
                    boFang.setText("||");
                } else {
                    player.pause();
                    boFang.setText("▶");
                }
                text.setText(list.get(id).substring(34, list.get(id).length() - 4));
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.stop();
                player.reset();
                bar.setProgress(0);
                id = id + 1;
                if (id > list.size() - 1) {
                    list.clear();
                    id = 0;
                }
                try {
                    player.setDataSource(list.get(id));
                    player.prepare();
                    player.seekTo(0);
                    player.start();
                    boFang.setText("||");
                    text.setText(list.get(id).substring(34, list.get(id).length() - 4));
                } catch (IOException IOE) {
                }
                totalTime = player.getDuration();
                bar.setMax(totalTime);
                if(fen(miao(totalTime)) < 10 && fenMiao(miao(totalTime)) < 10) {
                    totalText.setText("0" + fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
                } else if(fen(miao(totalTime)) < 10) {
                    totalText.setText("0" + fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
                } else if(fenMiao(miao(totalTime)) < 10) {
                    totalText.setText(fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
                } else {
                    totalText.setText(fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
                }
            }
        });
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.stop();
                player.reset();
                bar.setProgress(0);
                id = id - 1;
                if (id < 0) {
                    id = list.size() - 1;
                }
                try {
                    player.setDataSource(list.get(id));
                    player.prepare();
                    player.seekTo(0);
                    player.start();
                    boFang.setText("||");
                    text.setText(list.get(id).substring(34, list.get(id).length() - 4));
                } catch (IOException IOE) {
                }
                totalTime = player.getDuration();
                bar.setMax(totalTime);
                if(fen(miao(totalTime)) < 10 && fenMiao(miao(totalTime)) < 10) {
                    totalText.setText("0" + fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
                } else if(fen(miao(totalTime)) < 10) {
                    totalText.setText("0" + fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
                } else if(fenMiao(miao(totalTime)) < 10) {
                    totalText.setText(fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
                } else {
                    totalText.setText(fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
                }
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.stop();
                boFang.setText("▶");
                try {
                    player.reset();
                    player.setDataSource(list.get(id));
                    player.prepare();
                    text.setText(list.get(id).substring(34, list.get(id).length() - 4));
                } catch (IOException IOE) {
                }
            }

        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.exit(0);
            }
        });

        suiJi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.stop();
                player.reset();

                id = ra.nextInt(file.length);
                try {
                    player.setDataSource(list.get(id));
                    player.prepare();
                    player.seekTo(0);
                    player.start();
                    boFang.setText("||");
                    text.setText(list.get(id).substring(34, list.get(id).length() - 4));
                } catch (IOException IOE) {
                }
                totalTime = player.getDuration();
                bar.setMax(totalTime);
                if(fen(miao(totalTime)) < 10 && fenMiao(miao(totalTime)) < 10) {
                    totalText.setText("0" + fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
                } else if(fen(miao(totalTime)) < 10) {
                    totalText.setText("0" + fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
                } else if(fenMiao(miao(totalTime)) < 10) {
                    totalText.setText(fen(miao(totalTime)) + ":0" + fenMiao(miao(totalTime)));
                } else {
                    totalText.setText(fen(miao(totalTime)) + ":" + fenMiao(miao(totalTime)));
                }

            }
        });

        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser == true) {
                    player.seekTo(progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {

            }
        });
    }
    public int miao(int haoMiao) {
        return haoMiao / 1000;
    }

    public int fen(int miao) {
        return miao / 60;
    }

    public int fenh(int haoMiao) {
        return (haoMiao / 1000) / 60;
    }

    public int fenMiao(int miao) {
        return  miao % 60;
    }
}
